//****************************************************************
//* Copyright (c) 2008 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   ItemDao.java  $$
//* $$Revision:   1.1  $$
//* $$Author:   JLOPE262  $$
//* $$Date:   Nov 21 2008 14:24:54  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.persistence.dao;

import java.util.Collection;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc06Item;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import ar.com.ford.it.webengine.persistence.PersistenceUtil;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceException;
import com.ford.it.persistence.PersistenceManager;
import com.ford.it.persistence.Query;
import com.ford.it.persistence.Transaction;

public class ItemDao extends Dao {

    private static final String CLASS_NAME = ItemDao.class.getName();
    private static final ILogger log = LogFactory.getInstance().getLogger(
            CLASS_NAME);

    public Collection<Teesc06Item> findItems(Teesc06Item item)
            throws PersistenceException {

        String METHOD_NAME = "findItems";
        log.entering(CLASS_NAME, METHOD_NAME);

        Collection<Teesc06Item> res = null;
        PersistenceManager pm = null;

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findItems", Teesc06Item.class);
        query.setParameters(item);
        query.setOrdering("eesc06ItemNameN ascending");
        res = (Collection<Teesc06Item>) pm.executeQuery(query);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return res;
    }

    /**
     * Crea el Item pasado por parametro
     * 
     * @param item
     * Item a crear
     * @throws PersistenceException
     */
    public void createItem(Teesc06Item item) throws PersistenceException {

        String METHOD_NAME = "createItem";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = null;

        tx = PersistenceUtil.getTransaction();
        tx.registerInsert(item);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * Devuelve el Item cuyo Number es itemNumber listo para realizarle las
     * actualizaciones pertinentes
     * 
     * @param plant
     * Planta a la que pertenece el usuario que realiza la actualizacion
     * @param itemNumber
     * Number del Item a actualizar
     * @return
     * @throws PersistenceException
     */
    public Teesc06Item getItemForUpdate(Teesg01Plant plant, Integer itemNumber)
            throws PersistenceException {

        String METHOD_NAME = "getItemForUpdate";
        log.entering(CLASS_NAME, METHOD_NAME);

        Teesc06Item result = null;
        Teesc06Item pk = new Teesc06Item();

        pk.setEesc06PlantCodeC(plant);
        pk.setEesc06ItemNumberK(itemNumber);
        result = (Teesc06Item) super.getObjForUpdate(pk);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return result;
    }

    /**
     * Elimina el Item pasado por parametro
     * 
     * @param item
     * Item a eliminar
     * @throws PersistenceException
     */
    public void deleteItem(Teesc06Item item) throws PersistenceException {

        String METHOD_NAME = "deleteItem";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = PersistenceUtil.getTransaction();
        tx.registerDelete(item);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * Busca los items aprobados para la Item Category que contiene item. Si
     * Item Category es null entonces busca todos los items aprobados
     * 
     * @param item
     * @return
     * @throws PersistenceException
     */
    public Collection<Teesc06Item> findApprovedItems(Teesc06Item item)
            throws PersistenceException {

        String METHOD_NAME = "findApprovedItems";
        log.entering(CLASS_NAME, METHOD_NAME);

        Collection<Teesc06Item> res = null;
        PersistenceManager pm = null;

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findApprovedItems", Teesc06Item.class);
        query.setParameters(item);
        res = (Collection<Teesc06Item>) pm.executeQuery(query);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return res;
    }

}
